package com.ccl.keyassistant.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ccl.keyassistant.MainActivity;
import com.ccl.keyassistant.R;
import com.ccl.keyassistant.commons_codec.digest.DigestUtils;
import com.ccl.keyassistant.utils.L;
import com.ccl.keyassistant.utils.PermissionUtil;
import com.ccl.keyassistant.utils.ShareUtil;
import com.ccl.keyassistant.utils.StaticClass;
import com.ccl.keyassistant.view.CustomDialog;

/*
 *项目名     keyassistant
 *包名       com.ccl.keyassistant.activity
 *文件名     SplashActivity
 *创建者     ccl
 *创建时间   2019-03-07 16:20
 *注解       进入APP时显示的第一页（闪屏页）
 */

/**
 * 思路
 * 1 延时进入
 * 2 判断是否是第一次进入
 * 3 设置字体
 * 4 依据结果进行跳转
 */

public class SplashActivity extends AppCompatActivity {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //判断消息类别
            switch (msg.what) {
                case StaticClass.SPLASH_MSG:
                    if (isFirst()) {
                        //第一次运行，跳转到引导页
                        startActivity(new Intent(SplashActivity.this, GuideActivity.class));
                        finish();
                    } else {
                        //不是第一次运行，跳转到指纹识别
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        //从任务栈中销毁当前activity
                        finish();
                    }
                    break;
            }
        }
    };
    private TextView content;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        getWritePermission();
        initView();
    }

    private void initView() {
        content = (TextView) findViewById(R.id.tv_splash);
        //设置字体
        Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/HoeflerText.ttf");
        content.setTypeface(typeface);



    }

    private boolean isFirst() {
        boolean isFirstRuning = ShareUtil.getBoolean(this, "isFirstRuning", true);
        if(isFirstRuning){
            ShareUtil.putBoolean(this,"isFirstRuning",false);
        }
        return isFirstRuning;
    }

    /**
     * 获取权限
     */
    private void getWritePermission() {
        if (!PermissionUtil.isPermitted(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            PermissionUtil.requestPermission(SplashActivity.this, new String[]{
                    Manifest.permission.READ_PHONE_STATE
            }, StaticClass.PERMISSION_PHONE);
        }else {
            //2s后发送消息
            mHandler.sendEmptyMessageDelayed(StaticClass.SPLASH_MSG, 2000);
            getInfo();
        }
    }

    /**
     * 权限处理回调函数
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            //读写权限
            case StaticClass.PERMISSION_PHONE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //成功获取权限
                    Toast.makeText(this, "权限获取成功", Toast.LENGTH_SHORT).show();
                   getInfo();
                    //2s后发送消息
                    mHandler.sendEmptyMessageDelayed(StaticClass.SPLASH_MSG, 2000);
                } else {
                    //未获取权限
                    final CustomDialog dialog = new CustomDialog(this, R.layout.custom_dialog_ok);
                    dialog.show();

                    //利用inflate方式获取控件的点击事件是不生效的
//                    View view = View.inflate(this, R.layout.custom_dialog_ok, null);
                    //利用此方法可以
                    dialog.getWindow().findViewById(R.id.custom_dialog_ok).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            L.i("click");
                            dialog.dismiss();
                            PermissionUtil.requestPermission(SplashActivity.this,
                                    new String[]{Manifest.permission.READ_PHONE_STATE},
                                    StaticClass.PERMISSION_PHONE);
                        }
                    });
                }
                return;
            }
            default:
                break;

        }
    }

    private void getInfo() {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String deviceId = telephonyManager.getDeviceId();
        String hex = DigestUtils.sha1Hex(deviceId);
        StaticClass.MMSM = hex.substring(0,8);
    }
}
